home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / UsingPDF / GhostScript / source / gs5.10 / gscie.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-02  |  40.0 KB  |  1,190 lines

  1. /* Copyright (C) 1992, 1995, 1996, 1997 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gscie.c */
  20. /* CIE color rendering for Ghostscript */
  21. #include "math_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gsstruct.h"
  25. #include "gsmatrix.h"            /* for gscolor2.h */
  26. #include "gxcspace.h"
  27. #include "gscolor2.h"            /* for gs_set/currentcolorrendering */
  28. #include "gscie.h"
  29. #include "gxarith.h"
  30. #include "gxdevice.h"            /* for gxcmap.h */
  31. #include "gxcmap.h"
  32. #include "gzstate.h"
  33.  
  34. /* Forward references */
  35. private void cie_joint_caches_init(P3(gx_cie_joint_caches *,
  36.   const gs_cie_common *, const gs_cie_render *));
  37. private void cie_joint_caches_complete(P3(gx_cie_joint_caches *,
  38.   const gs_cie_common *, const gs_cie_render *));
  39. private void near cie_mult3(P3(const gs_vector3 *, const gs_matrix3 *,
  40.   gs_vector3 *));
  41. private void near cie_matrix_mult3(P3(const gs_matrix3 *, const gs_matrix3 *,
  42.   gs_matrix3 *));
  43. private void near cie_invert3(P2(const gs_matrix3 *, gs_matrix3 *));
  44. private void near cie_matrix_init(P1(gs_matrix3 *));
  45.  
  46. #define set_restrict_index(i, v, n)\
  47.   if ( (uint)(i = (int)(v)) >= (n) )\
  48.     i = (i < 0 ? 0 : (n) - 1)
  49. #define restrict_index(v, n, itemp)\
  50.   ((uint)(itemp = (int)(v)) >= (n) ?\
  51.    (itemp < 0 ? 0 : (n) - 1) : itemp)
  52.  
  53. /* Compute a cache index as (vin - base) * factor. */
  54. /* vin, base, factor, and the result are cie_cached_values. */
  55. /* We know that the result doesn't exceed (gx_cie_cache_size - 1) << fbits. */
  56. #define lookup_index(vin, pcache, fbits)\
  57.   ((vin) <= (pcache)->vecs.params.base ? 0 :\
  58.    (vin) >= (pcache)->vecs.params.limit ? (gx_cie_cache_size - 1) << (fbits) :\
  59.    cie_cached_product2int( ((vin) - (pcache)->vecs.params.base),\
  60.                (pcache)->vecs.params.factor, fbits ))
  61. #define lookup_value(vin, pcache)\
  62.   ((pcache)->vecs.values[lookup_index(vin, pcache, 0)])
  63.  
  64. #define if_restrict(v, range)\
  65.   if ( (v) < (range).rmin ) v = (range).rmin;\
  66.   else if ( (v) > (range).rmax ) v = (range).rmax
  67.  
  68. /* Define the template for loading a cache. */
  69. /* If we had parameterized types, or a more flexible type system, */
  70. /* this could be done with a single procedure. */
  71. #define cie_cache_init3(pcache3, plp3, prange3, cname)\
  72.     gs_cie_cache_init(&(pcache3)->floats.params, &(plp3)[0], &(prange3)[0], cname);\
  73.     gs_cie_cache_init(&(pcache3)[1].floats.params, &(plp3)[1], &(prange3)[1], cname);\
  74.     gs_cie_cache_init(&(pcache3)[2].floats.params, &(plp3)[2], &(prange3)[2], cname)
  75. #define CIE_LOAD_CACHE3_BODY(pcache, domains, rprocs, pcie, cname)\
  76. {    int i, j;\
  77.     gs_for_loop_params lp[3];\
  78.     cie_cache_init3(pcache, lp, domains, cname);\
  79.     for ( i = 0; i < gx_cie_cache_size; i++ )\
  80.       for ( j = 0; j < 3; lp[j].init += lp[j].step, j++ )\
  81.         pcache[j].floats.values[i] =\
  82.           (*(rprocs)->procs[j])(lp[j].init, pcie);\
  83. }
  84.  
  85. /* ================ Color space definition ================ */
  86.  
  87. /* Allocator structure types */
  88. private_st_joint_caches();
  89.  
  90. /* Define the CIE color space types. */
  91. /* We use CIExxx rather than CIEBasedxxx in some places because */
  92. /* gcc under VMS only retains 23 characters of procedure names, */
  93. /* and DEC C truncates all identifiers at 31 characters. */
  94. private cs_proc_concrete_space(gx_concrete_space_CIE);
  95. cs_declare_procs(private, gx_concretize_CIEDEFG, gx_install_CIEDEFG,
  96.   gx_adjust_cspace_CIEDEFG,
  97.   gx_enum_ptrs_CIEDEFG, gx_reloc_ptrs_CIEDEFG);
  98. cs_declare_procs(private, gx_concretize_CIEDEF, gx_install_CIEDEF,
  99.   gx_adjust_cspace_CIEDEF,
  100.   gx_enum_ptrs_CIEDEF, gx_reloc_ptrs_CIEDEF);
  101. private cs_proc_remap_color(gx_remap_CIEABC);
  102. cs_declare_procs(private, gx_concretize_CIEABC, gx_install_CIEABC,
  103.   gx_adjust_cspace_CIEABC,
  104.   gx_enum_ptrs_CIEABC, gx_reloc_ptrs_CIEABC);
  105. cs_declare_procs(private, gx_concretize_CIEA, gx_install_CIEA,
  106.   gx_adjust_cspace_CIEA,
  107.   gx_enum_ptrs_CIEA, gx_reloc_ptrs_CIEA);
  108. const gs_color_space_type
  109.     gs_color_space_type_CIEDEFG =
  110.      { gs_color_space_index_CIEDEFG, 3, true,
  111.        gx_init_paint_3, gx_concrete_space_CIE,
  112.        gx_concretize_CIEDEFG, NULL,
  113.        gx_default_remap_color, gx_install_CIEDEFG,
  114.        gx_adjust_cspace_CIEDEFG, gx_no_adjust_color_count,
  115.        gx_enum_ptrs_CIEDEFG, gx_reloc_ptrs_CIEDEFG
  116.      },
  117.     gs_color_space_type_CIEDEF =
  118.      { gs_color_space_index_CIEDEF, 3, true,
  119.        gx_init_paint_3, gx_concrete_space_CIE,
  120.        gx_concretize_CIEDEF, NULL,
  121.        gx_default_remap_color, gx_install_CIEDEF,
  122.        gx_adjust_cspace_CIEDEF, gx_no_adjust_color_count,
  123.        gx_enum_ptrs_CIEDEF, gx_reloc_ptrs_CIEDEF
  124.      },
  125.     gs_color_space_type_CIEABC =
  126.      { gs_color_space_index_CIEABC, 3, true,
  127.        gx_init_paint_3, gx_concrete_space_CIE,
  128.        gx_concretize_CIEABC, NULL,
  129.        gx_remap_CIEABC, gx_install_CIEABC,
  130.        gx_adjust_cspace_CIEABC, gx_no_adjust_color_count,
  131.        gx_enum_ptrs_CIEABC, gx_reloc_ptrs_CIEABC
  132.      },
  133.     gs_color_space_type_CIEA =
  134.      { gs_color_space_index_CIEA, 1, true,
  135.        gx_init_paint_1, gx_concrete_space_CIE,
  136.        gx_concretize_CIEA, NULL,
  137.        gx_default_remap_color, gx_install_CIEA,
  138.        gx_adjust_cspace_CIEA, gx_no_adjust_color_count,
  139.        gx_enum_ptrs_CIEA, gx_reloc_ptrs_CIEA
  140.      };
  141.  
  142. /* GC procedures for CIE color spaces. */
  143. /* These have to come after the cs_declare_procs because of */
  144. /* a bug in the VAX C compiler. */
  145.  
  146. #define pcs ((gs_color_space *)vptr)
  147.  
  148. private ENUM_PTRS_BEGIN(gx_enum_ptrs_CIEDEFG) return 0;
  149.     ENUM_PTR(0, gs_color_space, params.defg);
  150. ENUM_PTRS_END
  151. private RELOC_PTRS_BEGIN(gx_reloc_ptrs_CIEDEFG) {
  152.     RELOC_PTR(gs_color_space, params.defg);
  153. } RELOC_PTRS_END
  154.  
  155. private ENUM_PTRS_BEGIN(gx_enum_ptrs_CIEDEF) return 0;
  156.     ENUM_PTR(0, gs_color_space, params.def);
  157. ENUM_PTRS_END
  158. private RELOC_PTRS_BEGIN(gx_reloc_ptrs_CIEDEF) {
  159.     RELOC_PTR(gs_color_space, params.def);
  160. } RELOC_PTRS_END
  161.  
  162. private ENUM_PTRS_BEGIN(gx_enum_ptrs_CIEABC) return 0;
  163.     ENUM_PTR(0, gs_color_space, params.abc);
  164. ENUM_PTRS_END
  165. private RELOC_PTRS_BEGIN(gx_reloc_ptrs_CIEABC) {
  166.     RELOC_PTR(gs_color_space, params.abc);
  167. } RELOC_PTRS_END
  168.  
  169. private ENUM_PTRS_BEGIN(gx_enum_ptrs_CIEA) return 0;
  170.     ENUM_PTR(0, gs_color_space, params.a);
  171. ENUM_PTRS_END
  172. private RELOC_PTRS_BEGIN(gx_reloc_ptrs_CIEA) {
  173.     RELOC_PTR(gs_color_space, params.a);
  174. } RELOC_PTRS_END
  175.  
  176. #undef pcs
  177.  
  178. /* ------ Default values for CIE dictionary elements ------ */
  179.  
  180. /* Default transformation procedures. */
  181.  
  182. private float
  183. a_identity(floatp in, const gs_cie_a *pcie)
  184. {    return in;
  185. }
  186. private float
  187. abc_identity(floatp in, const gs_cie_abc *pcie)
  188. {    return in;
  189. }
  190. private float
  191. def_identity(floatp in, const gs_cie_def *pcie)
  192. {    return in;
  193. }
  194. private float
  195. defg_identity(floatp in, const gs_cie_defg *pcie)
  196. {    return in;
  197. }
  198. private float
  199. common_identity(floatp in, const gs_cie_common *pcie)
  200. {    return in;
  201. }
  202. private float
  203. render_identity(floatp in, const gs_cie_render *pcie)
  204. {    return in;
  205. }
  206. private float
  207. tpqr_identity(floatp in, const gs_cie_wbsd *pwbsd, const gs_cie_render *pcie)
  208. {    return in;
  209. }
  210. private frac
  211. render_table_identity(byte in, const gs_cie_render *pcie)
  212. {    return byte2frac(in);
  213. }
  214.  
  215. /* Default vectors and matrices. */
  216.  
  217. const gs_range3 Range3_default = { {{0,1}, {0,1}, {0,1}} };
  218. const gs_range4 Range4_default = { {{0,1}, {0,1}, {0,1}, {0,1}} };
  219. const gs_cie_defg_proc4 DecodeDEFG_default =
  220.  { { defg_identity, defg_identity, defg_identity, defg_identity } };
  221. const gs_cie_def_proc3 DecodeDEF_default =
  222.  { { def_identity, def_identity, def_identity } };
  223. const gs_cie_abc_proc3 DecodeABC_default =
  224.  { { abc_identity, abc_identity, abc_identity } };
  225. const gs_cie_common_proc3 DecodeLMN_default =
  226.  { { common_identity, common_identity, common_identity } };
  227. const gs_matrix3 Matrix3_default = { {1,0,0}, {0,1,0}, {0,0,1}, 1 /*true*/ };
  228. const gs_range RangeA_default = {0,1};
  229. const gs_cie_a_proc DecodeA_default = a_identity;
  230. const gs_vector3 MatrixA_default = { 1, 1, 1 };
  231. const gs_vector3 BlackPoint_default = { 0, 0, 0 };
  232. const gs_cie_render_proc3 Encode_default =
  233.  { { render_identity, render_identity, render_identity } };
  234. const gs_cie_transform_proc3 TransformPQR_default =
  235.  { { tpqr_identity, tpqr_identity, tpqr_identity } };
  236. const gs_cie_render_table_procs RenderTableT_default =
  237.  { { render_table_identity, render_table_identity, render_table_identity,
  238.      render_table_identity
  239.  } };
  240.  
  241. /* ------ Adjust reference counts for a CIE color space ------ */
  242.  
  243. private void
  244. gx_adjust_cspace_CIEDEFG(const gs_color_space *pcs, gs_memory_t *mem,
  245.   int delta)
  246. {    rc_adjust_const(pcs->params.defg, delta, "gx_adjust_cspace_CIEDEFG");
  247. }
  248.  
  249. private void
  250. gx_adjust_cspace_CIEDEF(const gs_color_space *pcs, gs_memory_t *mem,
  251.   int delta)
  252. {    rc_adjust_const(pcs->params.def, delta, "gx_adjust_cspace_CIEDEF");
  253. }
  254.  
  255. private void
  256. gx_adjust_cspace_CIEABC(const gs_color_space *pcs, gs_memory_t *mem,
  257.   int delta)
  258. {    rc_adjust_const(pcs->params.abc, delta, "gx_adjust_cspace_CIEABC");
  259. }
  260.  
  261. private void
  262. gx_adjust_cspace_CIEA(const gs_color_space *pcs, gs_memory_t *mem,
  263.   int delta)
  264. {    rc_adjust_const(pcs->params.a, delta, "gx_adjust_cspace_CIEA");
  265. }
  266.  
  267. /* ================ Table setup ================ */
  268.  
  269. /* ------ Install a CIE color space ------ */
  270.  
  271. private int cie_load_common_cache(P3(gs_cie_common *, gs_state *,
  272.   client_name_t));
  273. private void near cie_cache_mult(P3(gx_cie_vector_cache *, const gs_vector3 *,
  274.                     const cie_cache_floats *));
  275. private bool near cie_cache_mult3(P2(gx_cie_vector_cache *,
  276.                      const gs_matrix3 *));
  277.  
  278. private int
  279. gx_install_CIEDEFG(gs_color_space *pcs, gs_state *pgs)
  280. {    /****** NOT IMPLEMENTED YET ******/
  281.     return_error(gs_error_undefined);
  282. }
  283.  
  284. private int
  285. gx_install_CIEDEF(gs_color_space *pcs, gs_state *pgs)
  286. {    /****** NOT IMPLEMENTED YET ******/
  287.     return_error(gs_error_undefined);
  288. }
  289.  
  290. private int
  291. gx_install_CIEABC(gs_color_space *pcs, gs_state *pgs)
  292. {    gs_cie_abc *pcie = pcs->params.abc;
  293.     cie_matrix_init(&pcie->MatrixABC);
  294.     CIE_LOAD_CACHE3_BODY(pcie->caches.DecodeABC, pcie->RangeABC.ranges,
  295.                  &pcie->DecodeABC, pcie, "DecodeABC");
  296.     gs_cie_abc_complete(pcie);
  297.     return cie_load_common_cache(&pcie->common, pgs, "gx_install_CIEABC");
  298. }
  299.  
  300. private int
  301. gx_install_CIEA(gs_color_space *pcs, gs_state *pgs)
  302. {    gs_cie_a *pcie = pcs->params.a;
  303.     int i;
  304.     gs_for_loop_params lp;
  305.     float in;
  306.     gs_cie_cache_init(&pcie->caches.DecodeA.floats.params, &lp,
  307.               &pcie->RangeA, "DecodeA");
  308.     for ( i = 0, in = lp.init; i < gx_cie_cache_size; in += lp.step, i++ )
  309.       pcie->caches.DecodeA.floats.values[i] =
  310.         (*pcie->DecodeA)(in, pcie);
  311.     gs_cie_a_complete(pcie);
  312.     return cie_load_common_cache(&pcie->common, pgs, "gx_install_CIEA");
  313. }
  314.  
  315. /* Load the common caches when installing the color space. */
  316. private int
  317. cie_load_common_cache(gs_cie_common *pcie, gs_state *pgs, client_name_t cname)
  318. {    gx_cie_joint_caches *pjc;
  319.  
  320.     cie_matrix_init(&pcie->MatrixLMN);
  321.     CIE_LOAD_CACHE3_BODY(pcie->caches.DecodeLMN, pcie->RangeLMN.ranges,
  322.                  &pcie->DecodeLMN, pcie, "DecodeLMN")
  323.     if ( pgs->cie_render == 0 )
  324.       return 0;
  325.     pjc = gx_currentciecaches(pgs);
  326.     if ( pjc == 0 )
  327.       return_error(gs_error_VMerror);
  328.     cie_joint_caches_init(pjc, pcie, pgs->cie_render);
  329.     cie_joint_caches_complete(pjc, pcie, pgs->cie_render);
  330.     return 0;
  331. }
  332.  
  333. /* Complete loading a CIEBasedABC color space. */
  334. /* This routine is not idempotent. */
  335. void
  336. gs_cie_abc_complete(gs_cie_abc *pcie)
  337. {    pcie->caches.skipABC =
  338.       cie_cache_mult3(pcie->caches.DecodeABC, &pcie->MatrixABC);
  339. }
  340.  
  341. /* Complete loading a CIEBasedA color space. */
  342. /* This routine is not idempotent. */
  343. void
  344. gs_cie_a_complete(gs_cie_a *pcie)
  345. {    cie_cache_mult(&pcie->caches.DecodeA, &pcie->MatrixA,
  346.                &pcie->caches.DecodeA.floats);
  347. }
  348.  
  349. /* Convert a scalar cache to a vector cache by multiplying */
  350. /* the scalar values by a vector. */
  351. private void near
  352. cie_cache_mult(gx_cie_vector_cache *pcache, const gs_vector3 *pvec,
  353.   const cie_cache_floats *pcf)
  354. {    int i;
  355.     cie_vector_cache_params params;
  356.     params.is_identity = pcf->params.is_identity;
  357.     params.base = float2cie_cached(pcf->params.base);
  358.     params.factor = float2cie_cached(pcf->params.factor);
  359.     params.limit =
  360.       float2cie_cached((gx_cie_cache_size - 1) / pcf->params.factor +
  361.                pcf->params.base);
  362.     /* Loop from top to bottom so that we don't */
  363.     /* overwrite elements before they're used, */
  364.     /* in case pcf is an alias for pcache->floats. */
  365.     for ( i = gx_cie_cache_size; --i >= 0; )
  366.       {    float f = pcf->values[i];
  367.         pcache->vecs.values[i].u = float2cie_cached(f * pvec->u);
  368.         pcache->vecs.values[i].v = float2cie_cached(f * pvec->v);
  369.         pcache->vecs.values[i].w = float2cie_cached(f * pvec->w);
  370.       }
  371.     pcache->vecs.params = params;
  372. }
  373. /* Convert 3 scalar caches to vector caches by multiplying by a matrix. */
  374. /* Return true iff the resulting cache is an identity transformation. */
  375. private bool near
  376. cie_cache_mult3(gx_cie_vector_cache *pc /*[3]*/, const gs_matrix3 *pmat)
  377. {    cie_cache_mult(pc, &pmat->cu, &pc->floats);
  378.     cie_cache_mult(pc + 1, &pmat->cv, &pc[1].floats);
  379.     cie_cache_mult(pc + 2, &pmat->cw, &pc[2].floats);
  380.     return pmat->is_identity & pc[0].vecs.params.is_identity &
  381.        pc[1].vecs.params.is_identity & pc[2].vecs.params.is_identity;
  382. }
  383.  
  384. /* ------ Install a rendering dictionary ------ */
  385.  
  386. /* setcolorrendering */
  387. int
  388. gs_setcolorrendering(gs_state *pgs, gs_cie_render *pcie)
  389. {    int code = gs_cie_render_init(pcie);
  390.     if ( code < 0 )
  391.       return code;
  392.     rc_assign(pgs->cie_render, pcie, "gs_setcolorrendering");
  393.     /* Load the caches. */
  394.     CIE_LOAD_CACHE3_BODY(pcie->caches.EncodeLMN, pcie->DomainLMN.ranges,
  395.                  &pcie->EncodeLMN, pcie, "EncodeLMN");
  396.     CIE_LOAD_CACHE3_BODY(pcie->caches.EncodeABC, pcie->DomainABC.ranges,
  397.                  &pcie->EncodeABC, pcie, "EncodeABC");
  398.     if ( pcie->RenderTable.lookup.table != 0 )
  399.       { int i, j, m = pcie->RenderTable.lookup.m;
  400.         gs_for_loop_params flp;
  401.         for ( j = 0; j < m; j++ )
  402.           gs_cie_cache_init(&pcie->caches.RenderTableT[j].fracs.params,
  403.                 &flp, &Range3_default.ranges[0],
  404.                 "RenderTableT");
  405.         /****** ASSUMES gx_cie_cache_size >= 256 ******/
  406.         for ( i = 0; i < 256; i++ )
  407.           for ( j = 0; j < m; j++ )
  408.         pcie->caches.RenderTableT[j].fracs.values[i] =
  409.           (*pcie->RenderTable.T.procs[j])((byte)i, pcie);
  410.       }
  411.     code = gs_cie_render_complete(pcie);
  412.     if ( code < 0 )
  413.       return code;
  414.     /* Initialize the joint caches if needed. */
  415.     gs_cie_cs_complete(pgs, true);
  416.     gx_unset_dev_color(pgs);
  417.     return code;
  418. }
  419.  
  420. /* currentcolorrendering */
  421. const gs_cie_render *
  422. gs_currentcolorrendering(const gs_state *pgs)
  423. {    return pgs->cie_render;
  424. }
  425.  
  426. /* Unshare (allocating if necessary) the joint caches. */
  427. gx_cie_joint_caches *
  428. gx_currentciecaches(gs_state *pgs)
  429. {    rc_unshare_struct(pgs->cie_joint_caches, gx_cie_joint_caches,
  430.               &st_joint_caches, pgs->memory,
  431.               return 0, "gx_currentciecaches");
  432.     return pgs->cie_joint_caches;
  433. }
  434.  
  435. /* ------ Compute the parameters for loading a cache. ------ */
  436. /* Sets base and factor. */
  437.  
  438. void
  439. gs_cie_cache_init(cie_cache_params *pcache, gs_for_loop_params *pflp,
  440.   const gs_range *domain, client_name_t cname)
  441. {    /*
  442.      * We need to map the values in the range
  443.      * [domain->rmin..domain->rmax].  However, if neither rmin
  444.      * nor rmax is zero and the function is non-linear,
  445.      * this can lead to anomalies at zero, which is the
  446.      * default value for CIE colors.  The "correct" way to
  447.      * approach this is to run the mapping functions on demand,
  448.      * but we don't want to deal with the complexities of the
  449.      * callbacks this would involve (especially in the middle of
  450.      * rendering images); instead, we adjust the range so that zero
  451.      * maps precisely to a cache slot.  Define:
  452.      *    a = domain->rmin;
  453.      *    b = domain->rmax;
  454.      *    R = b - a;
  455.      *    N = gx_cie_cache_size - 1;
  456.      *    f(v) = N(v-a)/R;
  457.      *    x = f(0).
  458.      * If x is not an integer, we can either increase b or
  459.      * decrease a to make it one.  In the former case, compute:
  460.      *    Kb = floor(x); R'b = N(0-a)/Kb; b' = a + R'b.
  461.      * In the latter case, compute:
  462.      *    Ka = ceiling(x-N); R'a = N(0-b)/Ka; a' = b - R'a.
  463.      * We choose whichever method stretches the range the least,
  464.      * i.e., the one whose R' value (R'a or R'b) is smaller.
  465.      */
  466.     double a = domain->rmin, b = domain->rmax;
  467.     double R = b - a;
  468. #define N (gx_cie_cache_size - 1)
  469.     double delta;
  470.     /* Adjust the range if necessary. */
  471.     if ( a < 0 && b >= 0 )
  472.       {    double x = -N * a / R;    /* must be > 0 */
  473.         double Kb = floor(x);        /* must be >= 0 */
  474.         double Ka = ceil(x) - N;    /* must be <= 0 */
  475.         if ( Kb == 0 || (Ka != 0 && -b / Ka < -a / Kb) )    /* use R'a */
  476.           R = -N * b / Ka, a = b - R;
  477.         else            /* use R'b */
  478.           R = -N * a / Kb, b = a + R;
  479.       }
  480.     delta = R / N;
  481. #ifdef CIE_CACHE_INTERPOLATE
  482.     pcache->base = a;        /* no rounding */
  483. #else
  484.     pcache->base = a - delta / 2;    /* so lookup will round */
  485. #endif
  486.     pcache->factor = (delta == 0 ? 0 : N / R);
  487.     if_debug4('c', "[c]cache %s 0x%lx base=%g, factor=%g\n",
  488.           (const char *)cname, (ulong)pcache,
  489.           pcache->base, pcache->factor);
  490.     pflp->init = a;
  491.     pflp->step = delta;
  492.     pflp->limit = b + delta / 2;
  493. }
  494.  
  495. /* ------ Complete a rendering structure ------ */
  496.  
  497. /* Compute values derived from the rendering structure parameters */
  498. /* other than the cached procedure values.  This routine is idempotent. */
  499. private void near cie_transform_range3(P3(const gs_range3 *,
  500.   const gs_matrix3 *, gs_range3 *));
  501. int
  502. gs_cie_render_init(gs_cie_render *pcie)
  503. {    gs_matrix3 PQR_inverse;
  504.     cie_matrix_init(&pcie->MatrixLMN);
  505.     cie_matrix_init(&pcie->MatrixABC);
  506.     cie_matrix_init(&pcie->MatrixPQR);
  507.     cie_invert3(&pcie->MatrixPQR, &PQR_inverse);
  508.     cie_matrix_mult3(&pcie->MatrixLMN, &PQR_inverse,
  509.              &pcie->MatrixPQR_inverse_LMN);
  510.     cie_transform_range3(&pcie->RangePQR, &pcie->MatrixPQR_inverse_LMN,
  511.                  &pcie->DomainLMN);
  512.     cie_transform_range3(&pcie->RangeLMN, &pcie->MatrixABC,
  513.                  &pcie->DomainABC);
  514.     cie_mult3(&pcie->points.WhitePoint, &pcie->MatrixPQR, &pcie->wdpqr);
  515.     cie_mult3(&pcie->points.BlackPoint, &pcie->MatrixPQR, &pcie->bdpqr);
  516.     return 0;
  517. }
  518.  
  519. /* Transform a set of ranges. */
  520. private void near
  521. cie_transform_range(const gs_range3 *in, floatp mu, floatp mv, floatp mw,
  522.   gs_range *out)
  523. {    float umin = mu * in->ranges[0].rmin,
  524.       umax = mu * in->ranges[0].rmax;
  525.     float vmin = mv * in->ranges[1].rmin,
  526.       vmax = mv * in->ranges[1].rmax;
  527.     float wmin = mw * in->ranges[2].rmin,
  528.       wmax = mw * in->ranges[2].rmax;
  529.     float temp;
  530. #define swap(x, y) temp = x, x = y, y = temp
  531.  
  532.     if ( umin > umax ) swap(umin, umax);
  533.     if ( vmin > vmax ) swap(vmin, vmax);
  534.     if ( wmin > wmax ) swap(wmin, wmax);
  535.     out->rmin = umin + vmin + wmin;
  536.     out->rmax = umax + vmax + wmax;
  537. #undef swap
  538. }
  539. private void near
  540. cie_transform_range3(const gs_range3 *in, const gs_matrix3 *mat,
  541.   gs_range3 *out)
  542. {    cie_transform_range(in, mat->cu.u, mat->cv.u, mat->cw.u,
  543.                 &out->ranges[0]);
  544.     cie_transform_range(in, mat->cu.v, mat->cv.v, mat->cw.v,
  545.                 &out->ranges[1]);
  546.     cie_transform_range(in, mat->cu.w, mat->cv.w, mat->cw.w,
  547.                 &out->ranges[2]);
  548. }
  549.  
  550. /* Complete the loading of the rendering caches. */
  551. /* Note that this routine may make non-idempotent changes to */
  552. /* the values in the caches. */
  553. private void near cie_cache_restrict(P2(cie_cache_floats *, const gs_range *));
  554. #define cie_cache_restrict3(pcache3, pr3)\
  555.     cie_cache_restrict(&(pcache3)[0].floats, &(pr3)->ranges[0]);\
  556.     cie_cache_restrict(&(pcache3)[1].floats, &(pr3)->ranges[1]);\
  557.     cie_cache_restrict(&(pcache3)[2].floats, &(pr3)->ranges[2])
  558. int
  559. gs_cie_render_complete(gs_cie_render *pcie)
  560. {    /* Since range restriction happens immediately after */
  561.     /* the cache lookup, we can save a step by restricting */
  562.     /* the values in the cache entries. */
  563.     cie_cache_restrict3(pcie->caches.EncodeLMN, &pcie->RangeLMN);
  564.     cie_cache_restrict3(pcie->caches.EncodeABC, &pcie->RangeABC);
  565.     /* If there is no lookup table, we want the final ABC values */
  566.     /* to be fracs; if there is a table, we want them to be */
  567.     /* appropriately scaled ints. */
  568.     pcie->MatrixABCEncode = pcie->MatrixABC;
  569.       {    int c;
  570.         double f;
  571.         for ( c = 0; c < 3; c++ )
  572.           {    gx_cie_scalar_cache *pcache =
  573.               &pcie->caches.EncodeABC[c];
  574.             if ( pcie->RenderTable.lookup.table == 0 )
  575.             {    cie_cache_restrict(&pcache->floats,
  576.                            &Range3_default.ranges[0]);
  577.                 gs_cie_cache_to_fracs(pcache);
  578.                 pcache->fracs.params.is_identity = false;
  579.             }
  580.             else
  581.             {    int i;
  582.                 int n = pcie->RenderTable.lookup.dims[c];
  583. #ifdef CIE_RENDER_TABLE_INTERPOLATE
  584. #  define scale_index(f, n, itemp)\
  585.      restrict_index(f * (1 << _cie_interpolate_bits),\
  586.             (n) << _cie_interpolate_bits, itemp)
  587. #else
  588.                 int m = pcie->RenderTable.lookup.m;
  589.                 int k =
  590.                   (c == 0 ? 1 : c == 1 ?
  591.                    m * pcie->RenderTable.lookup.dims[2] : m);
  592. #  define scale_index(f, n, itemp)\
  593.      (restrict_index(f, n, itemp) * k)
  594. #endif
  595.                 const gs_range *prange =
  596.                   pcie->RangeABC.ranges + c;
  597.                 /* Loop from top to bottom so that we don't */
  598.                 /* overwrite elements before they're used. */
  599.                 for ( i = gx_cie_cache_size; --i >= 0; )
  600.                 {    float v =
  601.                       (pcache->floats.values[i] -
  602.                        prange->rmin) * (n - 1) /
  603.                       (prange->rmax - prange->rmin)
  604. #ifndef CIE_RENDER_TABLE_INTERPOLATE
  605.                         + 0.5
  606. #endif
  607.                           ;
  608.                     int itemp;
  609.  
  610.                     if_debug5('c',
  611.                       "[c]cache[%d][%d] = %g => %g => %d\n",
  612.                       c, i, pcache->floats.values[i], v,
  613.                       scale_index(v, n, itemp));
  614.                     pcache->ints.values[i] =
  615.                       scale_index(v, n, itemp);
  616.                 }
  617.                 pcache->ints.params = pcache->floats.params;    /* (not necessary) */
  618.                 pcache->ints.params.is_identity = false;
  619. #undef scale_index
  620.             }
  621.           }
  622.         /* Fold the scaling of the EncodeABC cache index */
  623.         /* into MatrixABC. */
  624. #define mabc(i, t)\
  625.   f = pcie->caches.EncodeABC[i].floats.params.factor;\
  626.   pcie->MatrixABCEncode.cu.t *= f;\
  627.   pcie->MatrixABCEncode.cv.t *= f;\
  628.   pcie->MatrixABCEncode.cw.t *= f;\
  629.   pcie->EncodeABC_base[i] =\
  630.     float2cie_cached(pcie->caches.EncodeABC[i].floats.params.base * f)
  631.         mabc(0, u);
  632.         mabc(1, v);
  633.         mabc(2, w);
  634.         pcie->MatrixABCEncode.is_identity = 0;
  635.       }
  636. #undef mabc
  637.     cie_cache_mult3(pcie->caches.EncodeLMN, &pcie->MatrixABCEncode);
  638.     return 0;
  639. }
  640.  
  641. /* Apply a range restriction to one cache or 3 caches. */
  642. private void near
  643. cie_cache_restrict(cie_cache_floats *pcache, const gs_range *prange)
  644. {    int i;
  645.     for ( i = 0; i < gx_cie_cache_size; i++ )
  646.       if_restrict(pcache->values[i], *prange);
  647. }
  648.  
  649. /* Convert a cache from floats to fracs. */
  650. void
  651. gs_cie_cache_to_fracs(gx_cie_scalar_cache *pcache)
  652. {    int i;
  653.     /* Loop from bottom to top so that we don't */
  654.     /* overwrite elements before they're used. */
  655.     for ( i = 0; i < gx_cie_cache_size; ++i )
  656.       pcache->fracs.values[i] = float2frac(pcache->floats.values[i]);
  657.     pcache->fracs.params = pcache->floats.params;    /* (not necessary) */
  658. }
  659.  
  660. /* ------ Fill in the joint cache ------ */
  661.  
  662. /* If the current color space is a CIE space, or has a CIE base space, */
  663. /* return a pointer to the common part of the space; otherwise return 0. */
  664. const gs_cie_common *
  665. gs_cie_cs_common(gs_state *pgs)
  666. {    const gs_color_space *pcs = pgs->color_space;
  667.  
  668. sw:    switch ( pcs->type->index )
  669.       {
  670.       case gs_color_space_index_CIEABC:
  671.         return &pcs->params.abc->common;
  672.       case gs_color_space_index_CIEA:
  673.         return &pcs->params.a->common;
  674.       case gs_color_space_index_Separation:
  675.         pcs = (const gs_color_space *)&pcs->params.separation.alt_space;
  676.         goto sw;
  677.       case gs_color_space_index_Indexed:
  678.         pcs = gs_color_space_indexed_base_space(pcs);
  679.         goto sw;
  680.       case gs_color_space_index_Pattern:
  681.         /****** WHAT? ******/
  682.       default:
  683.         return 0;
  684.       }
  685. }
  686.  
  687. /* Finish loading the joint caches for the current color space. */
  688. void
  689. gs_cie_cs_complete(gs_state *pgs, bool init)
  690. {    const gs_cie_common *common = gs_cie_cs_common(pgs);
  691.  
  692.     if ( common )
  693.       { if ( init )
  694.           cie_joint_caches_init(pgs->cie_joint_caches, common,
  695.                     pgs->cie_render);
  696.         cie_joint_caches_complete(pgs->cie_joint_caches, common,
  697.                       pgs->cie_render);
  698.       }
  699. }
  700.  
  701. /* Compute values derived from the color space and rendering parameters */
  702. /* other than the cached procedure values.  This routine is idempotent. */
  703. private void
  704. cie_joint_caches_init(gx_cie_joint_caches *pjc,
  705.   const gs_cie_common *pcie, const gs_cie_render *pcier)
  706. {    pjc->points_sd.ws.xyz = pcie->points.WhitePoint;
  707.     cie_mult3(&pjc->points_sd.ws.xyz, &pcier->MatrixPQR,
  708.           &pjc->points_sd.ws.pqr);
  709.     pjc->points_sd.bs.xyz = pcie->points.BlackPoint;
  710.     cie_mult3(&pjc->points_sd.bs.xyz, &pcier->MatrixPQR,
  711.           &pjc->points_sd.bs.pqr);
  712.     pjc->points_sd.wd.xyz = pcier->points.WhitePoint;
  713.     pjc->points_sd.wd.pqr = pcier->wdpqr;
  714.     pjc->points_sd.bd.xyz = pcier->points.BlackPoint;
  715.     pjc->points_sd.bd.pqr = pcier->bdpqr;
  716.     cie_matrix_mult3(&pcier->MatrixPQR, &pcie->MatrixLMN,
  717.              &pjc->MatrixLMN_PQR);
  718.     /* Load the TransformPQR caches. */
  719.     { int i, j;
  720.       gs_for_loop_params lp[3];
  721.       cie_cache_init3(pjc->TransformPQR, lp, pcier->RangePQR.ranges,
  722.               "TransformPQR");
  723.       for ( i = 0; i < gx_cie_cache_size; i++ )
  724.         for ( j = 0; j < 3; lp[j].init += lp[j].step, j++ )
  725.           pjc->TransformPQR[j].floats.values[i] =
  726.         (*pcier->TransformPQR.procs[j])(lp[j].init, &pjc->points_sd, pcier);
  727.     }
  728. }
  729.  
  730. /* Complete the loading of the joint caches.  Note that this routine */
  731. /* may make non-idempotent changes to the values in the caches. */
  732. private void
  733. cie_joint_caches_complete(gx_cie_joint_caches *pjc,
  734.   const gs_cie_common *pcie, const gs_cie_render *pcier)
  735. {    int j;
  736.     cie_cache_restrict3(pjc->TransformPQR, &pcier->RangePQR);
  737.     for ( j = 0; j < 3; j++ )
  738.       cie_cache_mult(&pjc->DecodeLMN[j],
  739.              &pjc->MatrixLMN_PQR.cu + j,
  740.              &pcie->caches.DecodeLMN[j].floats);
  741.     pjc->skipLMN = pjc->MatrixLMN_PQR.is_identity &
  742.       pjc->DecodeLMN[0].vecs.params.is_identity &
  743.       pjc->DecodeLMN[1].vecs.params.is_identity &
  744.       pjc->DecodeLMN[2].vecs.params.is_identity;
  745.     pjc->skipPQR =
  746.       cie_cache_mult3(pjc->TransformPQR, &pcier->MatrixPQR_inverse_LMN);
  747. }
  748.  
  749. /* ================ Color rendering (using the caches) ================ */
  750.  
  751. private int near cie_remap_finish(P3(const cie_cached_vector3 *,
  752.                      frac *, const gs_imager_state *));
  753. private void near cie_lookup_mult3(P2(cie_cached_vector3 *,
  754.                       const gx_cie_vector_cache *));
  755. #ifdef DEBUG
  756. private void near
  757. cie_lookup_map3(cie_cached_vector3 *pvec,
  758.   const gx_cie_vector_cache *pc /*[3]*/, const char _ds *cname)
  759. {    if_debug5('c', "[c]lookup %s 0x%lx [%g %g %g]\n",
  760.           (const char *)cname, (ulong)pc,
  761.           cie_cached2float(pvec->u), cie_cached2float(pvec->v),
  762.           cie_cached2float(pvec->w));
  763.     cie_lookup_mult3(pvec, pc);
  764.     if_debug3('c', "        =[%g %g %g]\n",
  765.           cie_cached2float(pvec->u), cie_cached2float(pvec->v),
  766.           cie_cached2float(pvec->w));
  767. }
  768. #else
  769. #  define cie_lookup_map3(pvec, pc, cname) cie_lookup_mult3(pvec, pc)
  770. #endif
  771.  
  772. /* Determine the concrete space underlying a CIEBased space. */
  773. private const gs_color_space *
  774. gx_concrete_space_CIE(const gs_color_space *pcs, const gs_imager_state *pis)
  775. {    const gs_cie_render *pcie = pis->cie_render;
  776.     if ( pcie == 0 || pcie->RenderTable.lookup.table == 0 ||
  777.          pcie->RenderTable.lookup.m == 3
  778.        )
  779.       return gs_color_space_DeviceRGB();
  780.     else                /* pcie->RenderTable.lookup.m == 4 */
  781.       return gs_color_space_DeviceCMYK();
  782. }
  783.  
  784. /* Render a CIEBasedDEFG color. */
  785. private int
  786. gx_concretize_CIEDEFG(const gs_client_color *pc, const gs_color_space *pcs,
  787.   frac *pconc, const gs_imager_state *pis)
  788. {    /****** NOT IMPLEMENTED YET ******/
  789.     return_error(gs_error_undefined);
  790. }
  791.  
  792. /* Render a CIEBasedDEF color. */
  793. private int
  794. gx_concretize_CIEDEF(const gs_client_color *pc, const gs_color_space *pcs,
  795.   frac *pconc, const gs_imager_state *pis)
  796. {    /****** NOT IMPLEMENTED YET ******/
  797.     return_error(gs_error_undefined);
  798. }
  799.  
  800. /* Render a CIEBasedABC color. */
  801. /* We provide both remap and concretize, but only the former */
  802. /* needs to be efficient. */
  803. private int
  804. gx_remap_CIEABC(const gs_client_color *pc, const gs_color_space *pcs,
  805.   gx_device_color *pdc, const gs_imager_state *pis, gx_device *dev,
  806.   gs_color_select_t select)
  807. {    frac conc[4];
  808.     const gs_cie_abc *pcie = pcs->params.abc;
  809.     cie_cached_vector3 vec3;
  810.  
  811.     if_debug3('c', "[c]remap CIEABC [%g %g %g]\n",
  812.           pc->paint.values[0], pc->paint.values[1],
  813.           pc->paint.values[2]);
  814.     vec3.u = float2cie_cached(pc->paint.values[0]);
  815.     vec3.v = float2cie_cached(pc->paint.values[1]);
  816.     vec3.w = float2cie_cached(pc->paint.values[2]);
  817.  
  818.     /* Apply DecodeABC and MatrixABC. */
  819. #define vabc vec3
  820. #define vlmn vec3
  821.     if ( !pcie->caches.skipABC )
  822.       cie_lookup_map3(&vabc /*&vlmn*/, &pcie->caches.DecodeABC[0],
  823.               "Decode/MatrixABC");
  824. #undef vabc
  825.     switch ( cie_remap_finish(&vlmn, conc, pis) )
  826.       {
  827.       case 3:
  828.         if_debug3('c', "[c]=RGB [%g %g %g]\n",
  829.               frac2float(conc[0]), frac2float(conc[1]),
  830.                 frac2float(conc[2]));
  831.         gx_remap_concrete_rgb(conc[0], conc[1], conc[2], pdc, pis,
  832.                       dev, select);
  833.         return 0;
  834.       case 4:
  835.         if_debug4('c', "[c]=CMYK [%g %g %g %g]\n",
  836.               frac2float(conc[0]), frac2float(conc[1]),
  837.                 frac2float(conc[2]), frac2float(conc[3]));
  838.         gx_remap_concrete_cmyk(conc[0], conc[1], conc[2], conc[3],
  839.                        pdc, pis, dev, select);
  840.         return 0;
  841.       }
  842.     /* Can't happen. */
  843.     return_error(gs_error_unknownerror);
  844. #undef vlmn
  845. }
  846. private int
  847. gx_concretize_CIEABC(const gs_client_color *pc, const gs_color_space *pcs,
  848.   frac *pconc, const gs_imager_state *pis)
  849. {    const gs_cie_abc *pcie = pcs->params.abc;
  850.     cie_cached_vector3 vec3;
  851.  
  852.     if_debug3('c', "[c]concretize CIEABC [%g %g %g]\n",
  853.           pc->paint.values[0], pc->paint.values[1],
  854.           pc->paint.values[2]);
  855.     vec3.u = float2cie_cached(pc->paint.values[0]);
  856.     vec3.v = float2cie_cached(pc->paint.values[1]);
  857.     vec3.w = float2cie_cached(pc->paint.values[2]);
  858. #define vabc vec3
  859. #define vlmn vec3
  860.     if ( !pcie->caches.skipABC )
  861.       cie_lookup_map3(&vabc /*&vlmn*/, &pcie->caches.DecodeABC[0],
  862.               "Decode/MatrixABC");
  863. #undef vabc
  864.     cie_remap_finish(&vlmn, pconc, pis);
  865. #undef vlmn
  866.     return 0;
  867. }
  868.  
  869. /* Render a CIEBasedA color. */
  870. private int
  871. gx_concretize_CIEA(const gs_client_color *pc, const gs_color_space *pcs,
  872.   frac *pconc, const gs_imager_state *pis)
  873. {    const gs_cie_a *pcie = pcs->params.a;
  874.     cie_cached_value a = float2cie_cached(pc->paint.values[0]);
  875.     cie_cached_vector3 vlmn;
  876.  
  877.     if_debug1('c', "[c]concretize CIEA %g\n", pc->paint.values[0]);
  878.  
  879.     /* Apply DecodeA and MatrixA. */
  880.     vlmn = lookup_value(a, &pcie->caches.DecodeA);
  881.     return cie_remap_finish(&vlmn, pconc, pis);
  882. }
  883.  
  884. /* Common rendering code. */
  885. /* Return 3 if RGB, 4 if CMYK. */
  886. private int near
  887. cie_remap_finish(const cie_cached_vector3 *plmn, frac *pconc,
  888.   const gs_imager_state *pis)
  889. {    const gs_cie_render *pcie = pis->cie_render;
  890.     const gx_cie_joint_caches *pjc = pis->cie_joint_caches;
  891.     const gs_const_string *table;
  892.     cie_cached_vector3 vec3;
  893.     int tabc[3];        /* indices for final EncodeABC lookup */
  894.  
  895.     if ( pcie == 0 )
  896.       {    /* No rendering has been defined yet. */
  897.         /* Just return black. */
  898.         pconc[0] = pconc[1] = pconc[2] = frac_0;
  899.         return 3;
  900.       }
  901.  
  902.     /* Apply DecodeLMN, MatrixLMN(decode), and MatrixPQR. */
  903. #define vlmn vec3
  904.     vlmn = *plmn;
  905. #define vpqr vec3
  906.     if ( !pjc->skipLMN )
  907.       cie_lookup_map3(&vlmn /*&vpqr*/, &pjc->DecodeLMN[0],
  908.               "Decode/MatrixLMN+MatrixPQR");
  909. #undef vlmn
  910.  
  911.     /* Apply TransformPQR, MatrixPQR', and MatrixLMN(encode). */
  912. #define vlmn vec3
  913.     if ( !pjc->skipPQR )
  914.       cie_lookup_map3(&vpqr /*&vlmn*/, &pjc->TransformPQR[0],
  915.               "Transform/Matrix'PQR+MatrixLMN");
  916. #undef vpqr
  917.  
  918.     /* Apply EncodeLMN and MatrixABC(encode). */
  919. #define vabc vec3
  920.     cie_lookup_map3(&vlmn /*&vabc*/, &pcie->caches.EncodeLMN[0],
  921.             "EncodeLMN+MatrixABC");
  922. #undef vlmn
  923.     /* MatrixABCEncode includes the scaling of the EncodeABC */
  924.     /* cache index. */
  925. #define set_tabc(i, t)\
  926.   set_restrict_index(tabc[i],\
  927.              cie_cached2int(vabc.t - pcie->EncodeABC_base[i],\
  928.                     _cie_interpolate_bits),\
  929.              gx_cie_cache_size << _cie_interpolate_bits)
  930.     set_tabc(0, u);
  931.     set_tabc(1, v);
  932.     set_tabc(2, w);
  933.     table = pcie->RenderTable.lookup.table;
  934.     if ( table == 0 )
  935.     {    /* No further transformation. */
  936.         /* The final mapping step includes both restriction to */
  937.         /* the range [0..1] and conversion to fracs. */
  938. #define eabc(i)\
  939.   cie_interpolate_fracs(pcie->caches.EncodeABC[i].fracs.values, tabc[i])
  940.         pconc[0] = eabc(0);
  941.         pconc[1] = eabc(1);
  942.         pconc[2] = eabc(2);
  943. #undef eabc
  944.         return 3;
  945.     }
  946.     else
  947.     {    /* Use the RenderTable. */
  948.         int m = pcie->RenderTable.lookup.m;
  949. #define rt_lookup(j, i) pcie->caches.RenderTableT[j].fracs.values[i]
  950. #ifdef CIE_RENDER_TABLE_INTERPOLATE
  951.  
  952.         /* The final mapping step includes restriction to the */
  953.         /* ranges [0..dims[c]] as ints with interpolation bits. */
  954.         fixed rfix[3];
  955.  
  956. #define eabc(i)\
  957.   cie_interpolate_fracs(pcie->caches.EncodeABC[i].ints.values, tabc[i])
  958. #define fabc(i)\
  959.   (eabc(i) << (_fixed_shift - _cie_interpolate_bits))
  960.         rfix[0] = fabc(0);
  961.         rfix[1] = fabc(1);
  962.         rfix[2] = fabc(2);
  963.         if_debug6('c', "[c]ABC=%g,%g,%g => iabc=%g,%g,%g\n",
  964.               cie_cached2float(vabc.u), cie_cached2float(vabc.v),
  965.               cie_cached2float(vabc.w), fixed2float(rfix[0]),
  966.               fixed2float(rfix[1]), fixed2float(rfix[2]));
  967.         gx_color_interpolate_linear(rfix, &pcie->RenderTable.lookup,
  968.                         pconc);
  969.         if_debug3('c', "[c]  interpolated => %g,%g,%g\n",
  970.               frac2float(pconc[0]), frac2float(pconc[1]),
  971.               frac2float(pconc[2]));
  972.         if ( !pcie->caches.RenderTableT_is_identity )
  973.           {    /* Map the interpolated values. */
  974. #define frac2cache_index(v) frac2bits(v, gx_cie_log2_cache_size)
  975.             pconc[0] = rt_lookup(0, frac2cache_index(pconc[0]));
  976.             pconc[1] = rt_lookup(1, frac2cache_index(pconc[1]));
  977.             pconc[2] = rt_lookup(2, frac2cache_index(pconc[2]));
  978.             if ( m > 3 )
  979.               pconc[3] = rt_lookup(3, frac2cache_index(pconc[3]));
  980. #undef frac2cache_index
  981.           }
  982.  
  983. #else                /* !CIE_RENDER_TABLE_INTERPOLATE */
  984.  
  985.         /* The final mapping step includes restriction to the */
  986.         /* ranges [0..dims[c]], plus scaling of the indices */
  987.         /* in the strings. */
  988. #define ri(i)\
  989.   pcie->caches.EncodeABC[i].ints.values[tabc[i] >> _cie_interpolate_bits]
  990.         int ia = ri(0);
  991.         int ib = ri(1);        /* pre-multiplied by m * NC */
  992.         int ic = ri(2);        /* pre-multiplied by m */
  993.         const byte *prtc = table[ia].data + ib + ic;
  994.             /* (*pcie->RenderTable.T)(prtc, m, pcie, pconc); */
  995.  
  996.         if_debug6('c', "[c]ABC=%g,%g,%g => iabc=%d,%d,%d\n",
  997.               cie_cached2float(vabc.u), cie_cached2float(vabc.v),
  998.               cie_cached2float(vabc.w), ia, ib, ic);
  999.         if ( pcie->caches.RenderTableT_is_identity )
  1000.           {    pconc[0] = byte2frac(prtc[0]);
  1001.             pconc[1] = byte2frac(prtc[1]);
  1002.             pconc[2] = byte2frac(prtc[2]);
  1003.             if ( m > 3 )
  1004.               pconc[3] = byte2frac(prtc[3]);
  1005.           }
  1006.         else
  1007.           {
  1008. #if gx_cie_log2_cache_size == 8
  1009. #  define byte2cache_index(b) (b)
  1010. #else
  1011. # if gx_cie_log2_cache_size > 8
  1012. #  define byte2cache_index(b)\
  1013.     ( ((b) << (gx_cie_log2_cache_size - 8)) +\
  1014.       ((b) >> (16 - gx_cie_log2_cache_size)) )
  1015. # else    /* < 8 */
  1016. #  define byte2cache_index(b) ((b) >> (8 - gx_cie_log2_cache_size))
  1017. # endif
  1018. #endif
  1019.             pconc[0] = rt_lookup(0, byte2cache_index(prtc[0]));
  1020.             pconc[1] = rt_lookup(1, byte2cache_index(prtc[1]));
  1021.             pconc[2] = rt_lookup(2, byte2cache_index(prtc[2]));
  1022.             if ( m > 3 )
  1023.               pconc[3] = rt_lookup(3, byte2cache_index(prtc[3]));
  1024. #undef byte2cache_index
  1025.           }
  1026.  
  1027. #endif                /* !CIE_RENDER_TABLE_INTERPOLATE */
  1028. #undef ri
  1029. #undef rt_lookup
  1030.         return m;
  1031.     }
  1032. }
  1033.  
  1034. /* ================ Utilities ================ */
  1035.  
  1036. #define if_debug_vector3(str, vec)\
  1037.   if_debug4('c', "%s[%g %g %g]\n", str, vec->u, vec->v, vec->w)
  1038. #define if_debug_matrix3(str, mat)\
  1039.   if_debug10('c', "%s[%g %g %g / %g %g %g / %g %g %g]\n", str,\
  1040.     mat->cu.u, mat->cu.v, mat->cu.w, mat->cv.u, mat->cv.v, mat->cv.w,\
  1041.     mat->cw.u, mat->cw.v, mat->cw.w)
  1042.  
  1043. /* Multiply a vector by a matrix. */
  1044. /* Note that we are computing M * V where v is a column vector. */
  1045. private void near
  1046. cie_mult3(const gs_vector3 *in, register const gs_matrix3 *mat,
  1047.   gs_vector3 *out)
  1048. {    if_debug_vector3("[c]mult", in);
  1049.     if_debug_matrix3("    *", mat);
  1050.     {    float u = in->u, v = in->v, w = in->w;
  1051.         out->u = (u * mat->cu.u) + (v * mat->cv.u) + (w * mat->cw.u);
  1052.         out->v = (u * mat->cu.v) + (v * mat->cv.v) + (w * mat->cw.v);
  1053.         out->w = (u * mat->cu.w) + (v * mat->cv.w) + (w * mat->cw.w);
  1054.     }
  1055.     if_debug_vector3("    =", out);
  1056. }
  1057.  
  1058. /* Multiply two matrices.  We assume the result is not an alias for */
  1059. /* either of the operands.  Note that the composition of the transformations */
  1060. /* M1 followed by M2 is M2 * M1, not M1 * M2.  (See gscie.h for details.) */
  1061. private void near
  1062. cie_matrix_mult3(const gs_matrix3 *ma, const gs_matrix3 *mb, gs_matrix3 *mc)
  1063. {    if_debug_matrix3("[c]matrix_mult", ma);
  1064.     if_debug_matrix3("             *", mb);
  1065.     cie_mult3(&mb->cu, ma, &mc->cu);
  1066.     cie_mult3(&mb->cv, ma, &mc->cv);
  1067.     cie_mult3(&mb->cw, ma, &mc->cw);
  1068.     cie_matrix_init(mc);
  1069.     if_debug_matrix3("             =", mc);
  1070. }
  1071.  
  1072. /* Invert a matrix. */
  1073. /* The output must not be an alias for the input. */
  1074. private void near
  1075. cie_invert3(register const gs_matrix3 *in, register gs_matrix3 *out)
  1076. {    /* This is a brute force algorithm; maybe there are better. */
  1077.     /* We label the array elements */
  1078.     /*   [ A B C ]   */
  1079.     /*   [ D E F ]   */
  1080.     /*   [ G H I ]   */
  1081. #define A cu.u
  1082. #define B cv.u
  1083. #define C cw.u
  1084. #define D cu.v
  1085. #define E cv.v
  1086. #define F cw.v
  1087. #define G cu.w
  1088. #define H cv.w
  1089. #define I cw.w
  1090.     double coA = in->E * in->I - in->F * in->H; 
  1091.     double coB = in->F * in->G - in->D * in->I; 
  1092.     double coC = in->D * in->H - in->E * in->G;
  1093.     double det = in->A * coA + in->B * coB + in->C * coC;
  1094.     if_debug_matrix3("[c]invert", in);
  1095.     out->A = coA / det;
  1096.     out->D = coB / det;
  1097.     out->G = coC / det;
  1098.     out->B = (in->C * in->H - in->B * in->I) / det;
  1099.     out->E = (in->A * in->I - in->C * in->G) / det;
  1100.     out->H = (in->B * in->G - in->A * in->H) / det;
  1101.     out->C = (in->B * in->F - in->C * in->E) / det;
  1102.     out->F = (in->C * in->D - in->A * in->F) / det;
  1103.     out->I = (in->A * in->E - in->B * in->D) / det;
  1104.     if_debug_matrix3("        =", out);
  1105. #undef A
  1106. #undef B
  1107. #undef C
  1108. #undef D
  1109. #undef E
  1110. #undef F
  1111. #undef G
  1112. #undef H
  1113. #undef I
  1114.     out->is_identity = in->is_identity;
  1115. }
  1116.  
  1117. /* Look up 3 values in a cache, with cached post-multiplication. */
  1118. private void near
  1119. cie_lookup_mult3(cie_cached_vector3 *pvec, const gx_cie_vector_cache *pc /*[3]*/)
  1120. {
  1121.     /****** Interpolating at intermediate stages doesn't seem to ******/
  1122.     /****** make things better, and slows things down, so....    ******/
  1123. #ifdef CIE_INTERPOLATE_INTERMEDIATE
  1124.     /* Interpolate between adjacent cache entries. */
  1125.     /* This is expensive! */
  1126. #ifdef CIE_CACHE_USE_FIXED
  1127. #  define lookup_interpolate_between(v0, v1, i, ftemp)\
  1128.      cie_interpolate_between(v0, v1, i)
  1129. #else
  1130.     float ftu, ftv, ftw;
  1131. #  define lookup_interpolate_between(v0, v1, i, ftemp)\
  1132.      ((v0) + ((v1) - (v0)) *\
  1133.       ((ftemp = float_rshift(i, _cie_interpolate_bits)), ftemp - (int)ftemp))
  1134. #endif
  1135.  
  1136.     cie_cached_value iu =
  1137.       lookup_index(pvec->u, pc, _cie_interpolate_bits);
  1138.     const cie_cached_vector3 *pu =
  1139.       &pc[0].vecs.values[(int)cie_cached_rshift(iu,
  1140.                             _cie_interpolate_bits)];
  1141.     const cie_cached_vector3 *pu1 =
  1142.       (iu >= (gx_cie_cache_size - 1) << _cie_interpolate_bits ?
  1143.        pu : pu + 1);
  1144.  
  1145.     cie_cached_value iv =
  1146.       lookup_index(pvec->v, pc + 1, _cie_interpolate_bits);
  1147.     const cie_cached_vector3 *pv =
  1148.       &pc[1].vecs.values[(int)cie_cached_rshift(iv,
  1149.                             _cie_interpolate_bits)];
  1150.     const cie_cached_vector3 *pv1 =
  1151.       (iv >= (gx_cie_cache_size - 1) << _cie_interpolate_bits ?
  1152.        pv : pv + 1);
  1153.  
  1154.     cie_cached_value iw =
  1155.       lookup_index(pvec->w, pc + 2, _cie_interpolate_bits);
  1156.     const cie_cached_vector3 *pw =
  1157.       &pc[2].vecs.values[(int)cie_cached_rshift(iw,
  1158.                             _cie_interpolate_bits)];
  1159.     const cie_cached_vector3 *pw1 =
  1160.       (iw >= (gx_cie_cache_size - 1) << _cie_interpolate_bits ?
  1161.        pw : pw + 1);
  1162.  
  1163.     pvec->u = lookup_interpolate_between(pu->u, pu1->u, iu, ftu) +
  1164.       lookup_interpolate_between(pv->u, pv1->u, iv, ftv) +
  1165.       lookup_interpolate_between(pw->u, pw1->u, iw, ftw);
  1166.     pvec->v = lookup_interpolate_between(pu->v, pu1->v, iu, ftu) +
  1167.       lookup_interpolate_between(pv->v, pv1->v, iv, ftv) +
  1168.       lookup_interpolate_between(pw->v, pw1->v, iw, ftw);
  1169.     pvec->w = lookup_interpolate_between(pu->w, pu1->w, iu, ftu) +
  1170.       lookup_interpolate_between(pv->w, pv1->w, iv, ftv) +
  1171.       lookup_interpolate_between(pw->w, pw1->w, iw, ftw);
  1172. #else
  1173.     const cie_cached_vector3 *pu = &lookup_value(pvec->u, pc);
  1174.     const cie_cached_vector3 *pv = &lookup_value(pvec->v, pc + 1);
  1175.     const cie_cached_vector3 *pw = &lookup_value(pvec->w, pc + 2);
  1176.     pvec->u = pu->u + pv->u + pw->u;
  1177.     pvec->v = pu->v + pv->v + pw->v;
  1178.     pvec->w = pu->w + pv->w + pw->w;
  1179. #endif
  1180. }
  1181.  
  1182. /* Set the is_identity flag that accelerates multiplication. */
  1183. private void near
  1184. cie_matrix_init(register gs_matrix3 *mat)
  1185. {    mat->is_identity =
  1186.       mat->cu.u == 1.0 && is_fzero2(mat->cu.v, mat->cu.w) &&
  1187.         mat->cv.v == 1.0 && is_fzero2(mat->cv.u, mat->cv.w) &&
  1188.           mat->cw.w == 1.0 && is_fzero2(mat->cw.u, mat->cw.v);
  1189. }
  1190.